package com.jiankangjin.jf360channel.services.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.jiankangjin.httpclient.HttpClient;
import com.jiankangjin.jf360channel.vo.RefundVo;

/**
 * 统一支付渠道 之 退款
 */
public class RefundServiceImpl {
	private static final transient Log log = LogFactory.getLog(RefundServiceImpl.class);
	
	public RefundVo default_V0_0_1 (
			String pay_ssn_id,
			String order_id,
			String total_fee,
			String pay_chnl_seq,
			String refund_fee,
			String currency,
			String expand_json) {
		RefundVo refundVo = new RefundVo();
		
		// 解开pay_chnl_seq 支付时，把userId存到数据库中了
		String userId = pay_chnl_seq.substring(0, 32);
		String accId = pay_chnl_seq.substring(32, 64);
		
		HashMap<String, Object> prams = new HashMap<String, Object>();
		prams.put("userId", userId);
		prams.put("rechargeAmount", Integer.parseInt(refund_fee));
		prams.put("accId", accId);
		prams.put("tradeId", pay_ssn_id);
		prams.put("expireTime", new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
		prams.put("appId", 11);//不能超过int!!!!
		prams.put("tradeResult", "success");
		prams.put("tradeFinTime", new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
		log.info("调帐户系统退款："+prams);
		Map<String, Object> map = HttpClient.getInstance().doPOSTgetHashMap("/accmanage/account_recharge", prams);
		log.info("帐户系统退款返回："+map);
		if(map == null || map.size()<=0) {
			refundVo.setResp_status("9999");
			refundVo.setResp_msg("系统故障");
			return refundVo;
		}
		String refund_msg = "";
		if ("0001".equals(map.get("status"))) {
			refundVo.setRefund_status("1002");//退款成功
			refund_msg = "返回成功，退款成功";
		} else {
			refundVo.setRefund_status("0042");//退款失败
			if ("1001".equals(map.get("status"))) {
				refund_msg = "令牌格式不合法，退款失败";
			} else if ("2001".equals(map.get("status"))) {
				refund_msg = "无此账户，退款失败";
			} else {
				refund_msg = "充值失败，退款失败";
			}
		}
		refundVo.setResp_status("0000");
		refundVo.setResp_msg("返回成功");
		refundVo.setPay_ssn_id(pay_ssn_id);
		refundVo.setPay_chnl_seq(pay_chnl_seq);
		refundVo.setRefund_fee(refund_fee);
		refundVo.setRefund_msg(refund_msg);
		refundVo.setSign("JF360Channel_refund_sign");
		return refundVo;
	}
}
